package com.lmy.springboot.filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 使用@WebFilter自定义一个 过滤器并注入spring 容器
 * 1.urlPatterns = {"/*"} 指定过滤器生效的路径
 */
@WebFilter(urlPatterns = {"/*"})
@Slf4j
public class LmyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("LmyFilter init()被调用...");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("LmyFilter doFilter()被调用...");
        //输入请求的uri资源
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String requestURI = httpServletRequest.getRequestURI();
        log.info("过滤器处理的uri={}", requestURI);
        //放行
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        log.info("LmyFilter destroy()被调用...");
    }
}
